package com.bigdata.appliance.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdata.appliance.common.exception.BusinessException;
import com.bigdata.appliance.common.exception.ResponseCode;
import com.bigdata.appliance.common.pojo.PageResult;
import com.bigdata.appliance.entity.Goods;
import com.bigdata.appliance.mapper.GoodsMapper;
import com.bigdata.appliance.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.appliance.vo.req.AddGoodsReqVO;
import com.bigdata.appliance.vo.req.EditGoodsReqVO;
import com.bigdata.appliance.vo.req.EditGoodsStatusReqVO;
import com.bigdata.appliance.vo.req.PageGoodsReqVO;
import com.bigdata.appliance.vo.resp.PageGoodsRespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author appliance-shop
 * @since 2023-12-24
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public void addGoods(AddGoodsReqVO vo) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(vo,goods);
        goods.setStatus(1);
        int result = goodsMapper.insert(goods);
        if (result != 1) {
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public void editGoods(EditGoodsReqVO vo) {
        Goods goods = goodsMapper.selectById(vo.getId());
        BeanUtils.copyProperties(vo,goods);
        int result = goodsMapper.updateById(goods);
        if (result != 1) {
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public void editGoods(EditGoodsStatusReqVO vo) {
        Goods goods = goodsMapper.selectById(vo.getId());
        BeanUtils.copyProperties(vo,goods);
        int result = goodsMapper.updateById(goods);
        if (result != 1) {
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public PageResult<PageGoodsRespVO> pageGoods(PageGoodsReqVO vo) {
        IPage<PageGoodsRespVO> result = goodsMapper.selectPageGoods(new Page<>(vo.getCurrent(), vo.getSize()), vo);
        return PageResult.getPage(result.getRecords(),result.getTotal());
    }
}
